package java4_7;


import java.io.*;
import java.util.*;
import java.util.List;


/**
 *  data.txt.
 * data.txt是由学生信息构成的数据文件，
 * 每行是一个学生的相关信息，包括姓名、班级和分数。姓名和其他信息之间用英文冒号隔开，
 * 班级和分数之间用英文逗号隔开，班级由“系名+班级序号”组成，如“计算191”。示例如下:
 * 王一:计算191,340
 * 张二:经济191,450.….(略)
 * 编程实现如下功能:
 * (1)读取data.txt，输出学生的姓名和分数到文件studs.txt,每行一条记录，
 * 姓名和分数用英文冒号隔开，示例如下:王—:340
 * 李四:450..(略)
 * (2）编程实现如下功能:
 * 选出分数最高的学生，打印输出学生的姓名和分数，中间用英文冒号隔开，示例如下:
 * 李四:450
 * (3）编程实现如下功能: 计算每个班级的平均分，打印输出班级和平均分，
 * 平均分小数点后保留2位，中间用英文冒号隔开，示例如下:
 * 计算191:447.55
 * 经济191:460.08
 */
public class PrintGrade {
    public void show(){
        String s1 = "F:\\Java83基础课件以及JDK安装包\\作业\\4.7\\data.txt";
        String s2 = "F:\\Java83基础课件以及JDK安装包\\作业\\4.7\\students.txt";
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(s1),"GBK"));
            BufferedWriter bw1 = new BufferedWriter(new FileWriter(s2));
            String s = null;
            String maxName = "xxx";
            int maxFen  = -1;
             //  班级名称   分数总和  人数总和
            Map<String, ClassAndCount> map = new HashMap<>();
            while ((s = br.readLine())!=null){
                //String test  = "李涵剑:经济191,430";

                String[] split = s.split(":");
                String name = split[0];
                String[] split1 = split[1].split(",");
                String classRoom = split1[0];
                int fen1 = Integer.parseInt(split1[1]);
//                int fen2 = Integer.parseInt(split1[1]);

                if (fen1 > maxFen){
                    maxName = name;
                    maxFen = fen1;
                }

                /*if (classRoom.equals("经济191"+":"+fen1+"\n")){
                    count1++;
                    grade1 = grade1+fen1;
                }else if (classRoom.equals("计算191"+":"+fen1+"\n")){
                    count2++;
                    grade2 = grade2+fen1;
                }*/


                if (map.containsKey(classRoom)){
                    ClassAndCount count = map.get(classRoom);
                    count.setNum(count.getNum()+1);
                    count.setSum(count.getSum()+fen1);
                }else {
                    ClassAndCount classAndCount = new ClassAndCount();
                    classAndCount.setNum(1);
                    classAndCount.setSum(fen1);
                    map.put(classRoom, classAndCount);
                }
                bw1.write(name+":"+fen1+"\n");
                bw1.flush();
            }
            Set<Map.Entry<String, ClassAndCount>> set = map.entrySet();
            Iterator<Map.Entry<String, ClassAndCount>> iterator  =  set.iterator();
            while (iterator.hasNext()){
                Map.Entry<String, ClassAndCount> entry = iterator.next();
                String className = entry.getKey();
                ClassAndCount classAndCount = entry.getValue();
                float avg =  (float)classAndCount.getSum()/classAndCount.getNum();
                //System.out.println(className+":"+avg);
                System.out.println(className+":"+String.format("%.2f", avg));
            }

            bw1.close();
            System.out.println("最高分"+maxFen+",姓名："+maxName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
